<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Attachment_control extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->model('upload_model');
		$this->load->model('download_model');
		$this->load->library('session');
	}

	function index($sid)
	{	
		if ( !$this->session->userdata('logged_in') ) {
		
			redirect('projekt/logged');
		
		}
		$mid = $this->session->flashdata('mid');

		$data = array(
				'mid' => $mid,
				'sid' => $sid
			);
		$this->session->set_flashdata($data);
		$this->session->keep_flashdata($data);

		$prilohy = $this->download_model->get_attachments($sid);

		$this->load->view('attachment', array('mid' => $mid, 'sid' => $sid, 'prilohy' => $prilohy));
	}

	function do_upload()
	{	

		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}
		
		$config['upload_path'] = './attachments2';
		$config['allowed_types'] = 'rbt|rar|zip|pdf';

		$this->load->library('upload', $config);

		$mid = $this->session->flashdata('mid');
		$sid = $this->session->flashdata('sid');
		$data = array(
				'mid' => $mid,
				'sid' => $sid
			);
		$this->session->set_flashdata($data);
		$this->session->keep_flashdata($data);

		if ( ! $this->upload->do_upload() )
		{	
			$error = $this->upload->display_errors();
			$mid = $this->session->flashdata('mid');
			$sid = $this->session->flashdata('sid');
			$data = array(
				'mid' => $mid,
				'sid' => $sid
			);
			$this->session->set_flashdata($data);
			$this->session->keep_flashdata($data);

			$prilohy = $this->download_model->get_attachments($sid);
			
			$this->load->view('attachment', array('error' => $error, 'mid' => $mid, 'sid' => $sid, 'prilohy' => $prilohy));
		}
		else
		{	
			$priloha = $this->upload->data();

			$cesta = 'http://kempelen.ii.fmph.uniba.sk/letnaliga/attachments2/'.$priloha['file_name'];

			chmod('./attachments2/'.$priloha['file_name'], 0755);

			$mid = $this->session->flashdata('mid');
			$sid = $this->session->flashdata('sid');

			$this->upload_model->upload_attachment($sid, $priloha['file_name']);

			$string = 'Príloha úspešne načítaná!<br/>Nahrajte ďalšiu, alebo stlačte Pokračovať.';

			$data = array(
				'mid' => $mid,
				'sid' => $sid
			);
			$this->session->set_flashdata($data);
			$this->session->keep_flashdata($data);

			$prilohy = $this->download_model->get_attachments($sid);

			$this->load->view('attachment', array('error' => $string, 'mid' => $mid, 'sid' => $sid, 'prilohy' => $prilohy));
		}
	}

		function delete_attachment($cesta)
	{
		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}

		$mid = $this->session->flashdata('mid');
		$sid = $this->session->flashdata('sid');
		$data = array(
				'mid' => $mid,
				'sid' => $sid
		);
		$this->session->set_flashdata($data);
		$this->session->keep_flashdata($data);
		unlink('./attachments2/'.$cesta);
		$this->upload_model->delete_attachment($sid, $cesta);

		$prilohy = $this->download_model->get_attachments($sid);
		$string = 'Príloha vymazaná.';

		$this->load->view('attachment', array('error' => $string, 'mid' => $mid, 'sid' => $sid, 'prilohy' => $prilohy));
	}
	}
?>